package avicit.bdp.dms.das.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.utils.BdpLogUtil;
import avicit.bdp.dms.das.dao.BdpDasLinkDao;
import avicit.bdp.dms.das.dto.BdpDasLinkDTO;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.core.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2020-06-24 14:54
 * @类说明：bdp_das_linkService
 * @修改记录：
 */
@Service
public class BdpDasLinkService extends BaseService<BdpDasLinkDao, BdpDasLinkDTO> {

    private static final Logger LOGGER = LoggerFactory.getLogger(BdpDasLinkService.class);

   

    /**
     * 新增对象
     *
     * @param bdpDasLinkDTO 保存对象
     * @return String
     */
    @Transactional
    public BdpDasLinkDTO insertBdpDasLink(BdpDasLinkDTO bdpDasLinkDTO) {
        try {
            bdpDasLinkDTO.setId(ComUtil.getId());
            insertSelective(bdpDasLinkDTO);
            //记录日志
            BdpLogUtil.log4Insert(bdpDasLinkDTO);
            return bdpDasLinkDTO;
        } catch (Exception e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }

    /**
     * 修改对象全部字段
     *
     * @param bdpDasLinkDTOs 修改对象
     * @return int
     */
    @Transactional
    public void updateBdpDasLink(List<BdpDasLinkDTO> bdpDasLinkDTOs) {
        try {
            for (BdpDasLinkDTO bdpDasLinkDTO : bdpDasLinkDTOs) {
                updateByPrimaryKey(getUpdateDto(bdpDasLinkDTO));
            }
        } catch (Exception e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }


    /**
     * 内部方法，获取修改的dto对象
     *
     * @param bdpDasLinkDTO
     * @return
     */
    private BdpDasLinkDTO getUpdateDto(BdpDasLinkDTO bdpDasLinkDTO) {
        BdpDasLinkDTO oldDTO = selectByPrimaryKey(bdpDasLinkDTO.getId());
        if (oldDTO == null) {
            throw new BusinessException("数据不存在");
        }
        //记录日志
        BdpLogUtil.log4Update(bdpDasLinkDTO, oldDTO);
        oldDTO.setSourceX(bdpDasLinkDTO.getSourceX());
        oldDTO.setSourceY(bdpDasLinkDTO.getSourceY());
        oldDTO.setTargetX(bdpDasLinkDTO.getTargetX());
        oldDTO.setTargetY(bdpDasLinkDTO.getTargetY());
        oldDTO.setSourceRelation(bdpDasLinkDTO.getSourceRelation());
        oldDTO.setTargetRelation(bdpDasLinkDTO.getTargetRelation());
        oldDTO.setLineType(bdpDasLinkDTO.getLineType());
        return oldDTO;
    }


    /**
     * 按主键删除
     *
     * @param ids
     */
    @Transactional
    public void deleteBdpDasLinkById(List<String> ids) {
        if (ids == null || ids.size() == 0) {
            throw new BusinessException("删除失败！传入的参数主键为null");
        }
        for (String id : ids) {
            try {
                //记录日志
                BdpDasLinkDTO bdpDasLinkDTO = selectByPrimaryKey(id);
                if (bdpDasLinkDTO == null) {
                    throw new BusinessException("删除失败！对象不存在");
                }
                BdpLogUtil.log4Delete(bdpDasLinkDTO);
                //删除业务数据
                deleteByPrimaryKey(id);
            } catch (Exception e) {
                throw new BusinessException(e.getMessage(), e);
            }
        }
    }


    /**
     * 按模型ID删除线关系
     *
     * @param tableId
     */
    public void deleteLinkByTableId(String tableId) {
    	this.mapper.deleteLinkByTableId(tableId);
    }


    /**
     * 根据字段id删除
     *
     * @param columnId
     */
    public void deleteLinkByColumnId(String columnId) {
    	this.mapper.deleteLinkByColumnId(columnId);
    }

    public  List<BdpDasLinkDTO> searchBdpDasLinkByTableId(List<String> tableQuery){
        return  this.mapper.searchBdpDasLinkByTableId(tableQuery);
    }
}